package com.dqp.cslggroup.JWC;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewStub;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import androidx.appcompat.widget.Toolbar;

import com.dqp.cslggroup.EventBus.MessageEvent;
import com.dqp.cslggroup.R;
import com.dqp.cslggroup.UI.BaseActivity;
import com.dqp.cslggroup.UI.MyApplication;
import com.dqp.cslggroup.Util.SharedPreferencesUtil;
import com.dqp.cslggroup.Util.ToastUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.IOException;
import java.util.List;
import java.util.Objects;

public class Tip_main extends BaseActivity {
    private String url;
    private String title;
    private String info;
    private String main;
    private String tip;
    private List<String> fj_url, fj_title;
    private TextView tip_title, tip_info, tip_main;
    private ViewStub viewstub_loading;
    private String Authorization = SharedPreferencesUtil.getStringSp(MyApplication.getContext(), "Authorization", "");

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tip_text);
        EventBus.getDefault().register(this);
        Toolbar toolbar = findViewById(R.id.include_toolbar);
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
        Intent intent = getIntent();
        url = intent.getStringExtra("url");
        initCmp();
        getTip();
    }

    private void initCmp() {
        tip_title = findViewById(R.id.tip_title);
        tip_info = findViewById(R.id.tip_zz);
        tip_main = findViewById(R.id.tz_text);
        viewstub_loading = findViewById(R.id.viewstub_loading);
    }

    @SuppressLint("HandlerLeak")
    public void getTip() {
        viewstub_loading.setVisibility(View.VISIBLE);
        new Thread(() -> {
            Connection.Response response;
            try {
                response = Jsoup.connect(url)
                        .ignoreContentType(true)
                        .userAgent("Mozilla")
                        .header("Authorization", Authorization)
                        .method(Connection.Method.GET)
                        .timeout(5000)
                        .execute();
                Document document = Jsoup.parse(response.body());
                title = document.select("div[class=sin_title]").text();
                info = document.select("div[class=sin_info]").text();
                main = document.select("div[class=sin_content]").text();
                fj_url = document.select("ul[style=list-style-type:none;]").select("li").select("a").eachAttr("href");
                fj_title = document.select("ul[style=list-style-type:none;]").select("li").select("a").eachText();
                EventBus.getDefault().post(new MessageEvent("Tip_main", 1, null));
            } catch (IOException e) {
                e.printStackTrace();
                EventBus.getDefault().post(new MessageEvent("Tip_main", 2, null));
            }
        }).start();

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0 && resultCode == RESULT_OK) {
            setResult(RESULT_OK);
            super.onDestroy();
            this.finish();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @SuppressLint("SetTextI18n")
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEvent event) {
        if (event.getEvent().equals("Tip_main")) {
            if (event.getWhat() == 1) {
                tip_info.setText(info);
                tip_title.setText(title);
                String[] x = main.split(" ");
                tip = x[0];
                for (int i = 1; i < x.length; i++) {
                    tip += "\n\t\t" + x[i];
                }
                tip_main.setText(tip);
                if (fj_title.size() > 0 && fj_title.size() == fj_url.size()) {
                    TextView fi_dow = findViewById(R.id.fj_dow);
                    fi_dow.setVisibility(View.VISIBLE);
                    ListView listView = findViewById(R.id.fj_list);
                    ArrayAdapter arrayAdapter = new ArrayAdapter<>(Tip_main.this, android.R.layout.simple_list_item_1, fj_title);
                    listView.setAdapter(arrayAdapter);
                    listView.setOnItemClickListener((adapterView, view, i, l) -> {
                        Uri uri = Uri.parse("http://www.cslggroup.top:8886" + fj_url.get(i));
                        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                        startActivity(intent);
                    });
                }
                viewstub_loading.setVisibility(View.GONE);
            }
            if (event.getWhat() == 2) {
                viewstub_loading.setVisibility(View.GONE);
                ToastUtil.showLongToast("网络请求超时！");
            }
            if (event.getWhat() == 3) {
                ListView listView = findViewById(R.id.fj_list);
                ArrayAdapter arrayAdapter = new ArrayAdapter<>(Tip_main.this, android.R.layout.simple_list_item_1, fj_title);
                listView.setAdapter(arrayAdapter);
                listView.setOnItemClickListener((adapterView, view, i, l) -> {
                    Uri uri = Uri.parse("http://www.cslggroup.top:8886" + fj_url.get(i));
                    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                    startActivity(intent);
                });
            }
        }
    }

}
